import { message } from 'antd';
import _ from 'lodash';

import api from '../../api';
import { isZiGuanEvir } from '../../utils/commonFunc';

export default {
  namespace: 'positionInquiryM',
  state: {
    columnsInfo: {
      // 详情和发行人基本信息
      1: { columns: [], checkedKeys: [], treeData: [] },
      2: { columns: [], checkedKeys: [], treeData: [] },
      31: { columns: [], checkedKeys: [], treeData: [] },
      32: { columns: [], checkedKeys: [], treeData: [] },
      4: { columns: [], checkedKeys: [], treeData: [] },
      5: { columns: [], checkedKeys: [], treeData: [] },
      6: { columns: [], checkedKeys: [], treeData: [] },
      7: { columns: [], checkedKeys: [], treeData: [] },
      8: { columns: [], checkedKeys: [], treeData: [] },
      9: { columns: [], checkedKeys: [], treeData: [] },
      10: { columns: [], checkedKeys: [], treeData: [] },
      11: { columns: [], checkedKeys: [], treeData: [] },
      111: { columns: [], checkedKeys: [], treeData: [] },
      112: { columns: [], checkedKeys: [], treeData: [] },
      12: { columns: [], checkedKeys: [], treeData: [] },
      13: { columns: [], checkedKeys: [], treeData: [] },
      14: { columns: [], checkedKeys: [], treeData: [] },
      15: { columns: [], checkedKeys: [], treeData: [] },
      16: { columns: [], checkedKeys: [], treeData: [] },
      17: { columns: [], checkedKeys: [], treeData: [] },
      18: { columns: [], checkedKeys: [], treeData: [] },
    },
    checkedKeysInfo: {
      // 详情和发行人基本信息
      1: { checkedKeys: [] },
      2: { checkedKeys: [] },
      31: { checkedKeys: [] },
      32: { checkedKeys: [] },
      4: { checkedKeys: [] },
      5: { checkedKeys: [] },
      6: { checkedKeys: [] },
      7: { checkedKeys: [] },
      8: { checkedKeys: [] },
      9: { checkedKeys: [] },
      10: { checkedKeys: [] },
      11: { checkedKeys: [] },
      111: { checkedKeys: [] },
      112: { checkedKeys: [] },
      12: { checkedKeys: [] },
      13: { checkedKeys: [] },
      14: { checkedKeys: [] },
      15: { checkedKeys: [] },
      16: { checkedKeys: [] },
      17: { checkedKeys: [] },
      18: { checkedKeys: [] },
    },
  },
  effects: {
    // 获取token值
    *saveToken({ payload }, { call }) {
      let data;
      let withMAAT = false;
      // 判断当前环境是不是资管
      const temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        data = yield call(api.saveTokenWITHMAAT, payload || {});
      } else {
        data = yield call(api.saveToken, payload || {});
      }
      if (data && data.success) {
        return data;
      }
      message.error('导出失败');
    },

    // 查询获取表头信息
    // eslint-disable-next-line no-unused-vars
    *queryShowCol({ payload, callback }, { call, put, select }) {
      let res;
      let withMAAT = false;
      // 判断当前环境是不是资管
      const temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        res = yield call(api.positionInquiry.queryShowColWITHMAAT, payload);
      } else {
        res = yield call(api.positionInquiry.queryShowCol, payload);
      }
      if (res && res.code == 0 && res.data.dataSource) {
        const { columns } = res.data.dataSource;
        const checkedKeys = [];
        const treeData = [
          {
            title: '全部',
            key: 'all',
            children: res.data.dataSource.columns,
          },
        ];

        res.data.dataSource.columns.map((item) => {
          if (item.display == true) {
            checkedKeys.push(item.key);
          }
        });

        if (payload.type == 'Trade_Ana_Bnd_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            1: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            1: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_Fnd_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            2: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            2: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_Fnd_Hold_Info_ETF') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            31: { columns: _.cloneDeep(columns), checkedKeys: _.cloneDeep(checkedKeys), treeData: _.cloneDeep(treeData) },
            32: { columns: _.cloneDeep(columns), checkedKeys: _.cloneDeep(checkedKeys), treeData: _.cloneDeep(treeData) },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            31: { checkedKeys: _.cloneDeep(checkedKeys) },
            32: { checkedKeys: _.cloneDeep(checkedKeys) },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_Deposit_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            4: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            4: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_Fmt_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            5: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            5: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_Yhjhg_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            6: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            6: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_Jyshg_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            7: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            7: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_DirectFinancing_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            8: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            8: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_Nonstandard_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            9: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            9: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'Trade_Ana_Xyshg_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            10: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            10: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'TRADE_ANA_STK_HOLD_INFO' || payload.type == 'TRADE_ANA_STK_HOLD_INFO_HK') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            11: { columns, checkedKeys, treeData },
            111: { columns: _.cloneDeep(columns), checkedKeys: _.cloneDeep(checkedKeys), treeData: _.cloneDeep(treeData) },
            112: { columns: _.cloneDeep(columns), checkedKeys: _.cloneDeep(checkedKeys), treeData: _.cloneDeep(treeData) },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            11: { checkedKeys },
            111: { checkedKeys: _.cloneDeep(checkedKeys) },
            112: { checkedKeys: _.cloneDeep(checkedKeys) },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (payload.type == 'trade_ana_lending_hold_info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            12: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            12: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        // 资产管理计划
        if (payload.type == 'Trade_Ana_Planinfo_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            13: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            13: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        // 资产管理计划
        if (payload.type == 'Trade_Ana_Outsource_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            14: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            14: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        // 股指期货
        if (payload.type == 'Trade_Ana_Futures_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            15: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            15: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        // 商品期货
        if (payload.type == 'Trade_Ana_Commodity_Future_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            16: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            16: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        // 期权
        if (payload.type == 'Trade_Ana_Option_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            17: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            17: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
        // 国债期货
        if (payload.type == 'Trade_Ana_Debt_Future_Hold_Info') {
          let columnsInfo = yield select((state) => state.positionInquiryM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.positionInquiryM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            18: { columns, checkedKeys, treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            18: { checkedKeys },
          };

          yield put({
            type: 'save',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
      } else {
        message.error(res?.message || '查询获取表头信息失败');
      }
    },

    // 保存表头信息
    *saveCol({ payload }, { call }) {
      const res = yield call(api.positionInquiry.saveCol, payload);
      return res;
      //   if(res && res.success){
      //     // callback(res.data)
      //     }else{
      //     message.error(res?.message ||'保存表头信息失败')
      //     }
    },

    // 获取统计信息
    *getSearchTotal({ payload, callback }, { call }) {
      let res;
      let withMAAT = false;
      // 判断当前环境是不是资管
      const temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        res = yield call(api.positionInquiry.getSearchTotalWITHMAAT, payload);
      } else {
        res = yield call(api.positionInquiry.getSearchTotal, payload);
      }
      if (res && res.code == '0') {
        callback(res.data);
      } else {
        message.error((res ? res?.message : '查询统计信息失败') || '查询统计信息失败');
      }
    },

    // 债券借贷持仓表
    *bondLendingTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.bondLendingTable, payload);
      if (res && res.success) {
        callback(res);
      } else if (res) {
        message.error(res?.message || '查询债券借贷失败');
      } else {
        message.error('查询债券借贷失败');
      }
    },
    // 股票持持仓表
    *stockTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.stockTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询股票持仓失败');
      }
    },

    // 债券持持仓表
    *bondTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.bondTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询债券持仓失败');
      }
    },

    // 场外基金持仓表
    *outsideFundsTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.outsideFundsTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询场外基金持仓失败');
      }
    },

    // ETF持仓表
    *etfTable({ payload, callback }, { call }) {
      let res;
      let withMAAT = false;
      // 判断当前环境是不是资管
      const temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) withMAAT = true;
      if (withMAAT) {
        res = yield call(api.positionInquiry.outsideFundsTableWITHMAAT, payload);
      } else {
        res = yield call(api.positionInquiry.outsideFundsTable, payload);
      }
      if (res && res.success) {
        callback(res);
      } else {
        message.error((res ? res?.message : '查询ETF持仓失败') || '查询ETF持仓失败');
      }
    },

    // 存款持仓表
    *depositMoneyTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.depositMoneyTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询存款持仓失败');
      }
    },

    // 理财/资产计划持仓表
    *assetplanTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.assetplanTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询理财/资产计划持仓失败');
      }
    },

    // 银行间回购持仓表
    *interbankRepoTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.interbankRepoTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询银行间回购失败');
      }
    },
    // 银行间买断式获取列表
    *interbankBuyOutTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.interbankBuyOutTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询银行间回购失败');
      }
    },
    // 交易所回购持仓表
    *exchangeRepoTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.exchangeRepoTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询交易所回购失败');
      }
    },

    // 交易所协议回购持仓表
    *exchangeXysRepoTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.exchangeXysRepoTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询交易所协议回购失败');
      }
    },

    // 理财直融持仓表
    *directFinancingTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.directFinancingTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询理财直融失败');
      }
    },

    // 非标持仓表
    *nonstandardTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.nonstandardTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询非标持仓失败');
      }
    },
    // 资产管理计划持仓表
    *planInfoTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.planInfoTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询资产管理计划持仓失败');
      }
    },
    // 委外投资持仓
    *outSourcingTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.outSourcingTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询委外投资持仓失败');
      }
    },
    // 股指期货
    *stockFutureTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.stockFutureTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询股指期货持仓失败');
      }
    },
    // 商品期货
    *shoopFutureTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.shoopFutureTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询商品期货持仓失败');
      }
    },
    // 国债期货
    *nationalFutureTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.nationalFutureTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询国债期货持仓失败');
      }
    },
    // 查询穿透信息
    *getChildTableData({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.getChildTableData, payload);
      if (res && res.code == '0') {
        callback(res.data?.dataSource || []);
      } else {
        callback([]);
        message.error(res?.message || '查询穿透信息失败');
      }
    },
    // 查询期权表格
    *queryOptionTable({ payload, callback }, { call }) {
      const res = yield call(api.positionInquiry.queryOptionTable, payload);
      if (res && res.success) {
        callback(res);
      } else {
        message.error(res?.message || '查询期权表格失败!');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
